package org.ichow.scenegame.view
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Point;
	import org.ichow.scenegame.core.Item;
	import org.ichow.scenegame.core.ToolTip;
	import org.ichow.scenegame.model.AssetsProxy;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author M.ichow
	 */
	public class ToolTipMediator extends Mediator
	{
		public static const NAME:String = "ToolTipMediator";
		public static const SHOW_TOOLTIP:String = "showTooltip";
		static public const HIDE_TOOLTIP:String = "hideTooltip";
		
		public static const NOTI_TOOLTIP:String = "notiTooltip";
		
		public function ToolTipMediator( tooltip:ToolTip )
		{
			super( NAME , tooltip );
		}
		
		override public function onRegister():void
		{
			super.onRegister();
			//
			var assets:AssetsProxy = facade.retrieveProxy( AssetsProxy.NAME ) as AssetsProxy;
			var def:Class = assets.getClass( "ToolTip" );
			tooltip.defSkin = def;
			//noti tooltip
			tooltip.createToolTip( NOTI_TOOLTIP , def , { maxWidth: 200 } );
		}
		
		override public function listNotificationInterests():Array
		{
			return [ SHOW_TOOLTIP , HIDE_TOOLTIP ];
		}
		
		override public function handleNotification( notification:INotification ):void
		{
			super.handleNotification( notification );
			var scene:SceneMediator = facade.retrieveMediator( SceneMediator.NAME ) as SceneMediator;
			var container:DisplayObjectContainer = scene.getViewComponent() as DisplayObjectContainer;
			var type:String = notification.getType();
			var tt:Sprite = tooltip.getToolTip( type );
			switch ( notification.getName() )
			{
				case SHOW_TOOLTIP: 
					var o:Item = notification.getBody() as Item;
					var txt:String = o.des;
					tooltip.setText( type , txt );
					var point:Point = new Point( o.x , o.y );
					point = o.parent.localToGlobal( point );
					
					if (( point.x + o.width + tt.width + 5 ) > int( scene.getValueForKey( "width" ) ) )
					{
						tt.x = Math.floor( point.x - tt.width );
					}
					else
					{
						tt.x = Math.floor( point.x + o.width );
					}
					tt.y = Math.floor( point.y + ( o.height - tt.height ) / 2 );
					container.addChild( tt );
					break;
				case HIDE_TOOLTIP: 
					if ( container.contains( tt ) )
						container.removeChild( tt );
					break;
				default: 
					break;
			}
		}
		
		public function getTooltip( type:String ):void
		{
			tooltip.getToolTip( type );
		}
		
		protected function get tooltip():ToolTip
		{
			return viewComponent as ToolTip;
		}
	
	}

}